<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/4
 * Time: 14:30
 */

namespace app\api\controller\v1;

use app\api\controller\BaseController;
use app\api\service\ToToken;
use app\api\validate\OrderPlace;
use app\api\validate\PagingParameter;
use app\api\model\Order as orderModel;
/*
 * @订单类
 * */
class Order extends BaseController
{
    //1.用户在选择商品后,向API提交包含他所选择的商品信息的相关信息
    //2.api在接收到消息后,需要检查订单相关商品的库存量
    //3.有库存,把订单数据存入数据库中.下单成功了,返回客户端消息,告诉可以支付
    //4.调用我们的支付接口,进行支付
    //5.还需要再次进行库存量的检测
    //6.服务器这边就可以调用微信的支付接口进行支付
    //7.微信返回支付结果(异步)
    //8成功,也要进行库存量的检测
    //9.成功,进行库存量的扣除,失败,返回一个支付失败的结果

    protected $beforeActionList = [
        'checkExclusiveScope'=>['only'=>'placeOrder'],
    ];

    public function getSummaryByUser($page = 1,$size = 15)
    {
        (new PagingParameter())->goCheck();
        $uid = ToToken::getCurrentUid();
        $pagingOrders = orderModel::getSummaryByUser($uid,$page,$size);
        $pagingOrders = collection($pagingOrders);
        if ($pagingOrders->isEmpty()){
            return [
                'data'=>[],
                'current_page'=>$pagingOrders->getCurrentPage(),
            ];
        }
        $data = $pagingOrders->toArray();
        return [
            'data'=>$data,
            'current_page'=>$pagingOrders->getCurrentPage(),
        ];
    }

    //下单接口
    public function placeOrder(){
        (new OrderPlace())->goCheck();
        $products = input('post.products/a');
        $uid = ToToken::getCurrentUid();

        $order = new \app\api\service\Order();
        $status = $order->place($uid,$products);
        return $status;
    }
}
